<?php
/*
 * dashboardRec.php
 * 
 * Recommendations dashboard
 * Show a dashboard highlighting surveys with unresolved recommendations
 * 
 * Copyright 2013 Eugene Mah <eugenemah@gmail.com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

require('functions.inc');

// Get the current date
date_default_timezone_set('America/New_York');
$today = date("Ymd");
$year = date("Y");
$month = date("m");
$day = date("d");

/*
 * Establish a connection to the database using PDO. Most of this is
 * taken from PHP in Action by Reiersol, Baker, Shiflett (Manning Publications)
 * and the PHP PDO documentation (http://php.net/manual/en/book.pdo.php)
 */
try {
  $conn = new PDO(
    'mysql:dbname=ComplianceReports;host=localhost',
    'eugenem','imabug');
}
catch (Exception $e) {
  throw new Exception($e->getMessage());
}
// Set PDO to throw more exceptions when encountered
$conn->setAttribute(PDO::ATTR_ERRMODE,
                    PDO::ERRMODE_EXCEPTION);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
  <title>untitled</title>
  <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  <meta name="generator" content="Geany 1.22" />
<?PHP

?>
<style type="text/css">
.red {background-color: #FF7474;}
.orange {background-color: #FFBD66;}
.yellow {background-color: #FFFF80;}
.green {background-color: #80FF80;}
td {text-align: center;}
</style>
</head>

<body>
<h1>Radiological Equipment Database</h1>
<h2>Equipment Recommendations Status Dashboard</h2>
<h3>Table legend</h3>
<table>
  <tr>
    <td class="green">No unresolved recommendations</td>
    <td class="yellow">Unresolved recommendations < 30 days</td>
    <td class="orange">Unresolved recommendations > 30 days</td>
    <td class="red">Unresolved recommendations > 60 days</td>
  </tr>
</table>
<?PHP
// Get the list of modalities
$modResult = mysql_query("select * from Modality");

// Cycle through each modality
while ($modality = mysql_fetch_array($modResult)) {
  // Fetch the list of equipment
  $query = "SELECT MachineID,Modality,Manufacturer,Description,Location
            FROM Modality,Manufacturer,Location,Machines
            WHERE Machines.ModalityID=".$modality['ModalityID']."
            AND Machines.Status=\"Active\"
            AND Machines.SurveyFreq<>\"NA\"
            AND Modality.ModalityID=Machines.ModalityID
            AND Manufacturer.ManufacturerID=Machines.ManufacturerID
            AND Location.LocationID=Machines.LocationID
            ORDER BY Description";
  $machResult = mysql_query($query)
    or die ("Query failed: ".$query."<br />\n");
  // Find out how many results were returned
  $machCount = mysql_num_rows($machResult);

  // Want to create a 5xn table to display the results. Figure out n
  $n = (int)ceil($machCount/5);
  // Create the table for this modality
  echo "<h3>".$modality['Modality']."</h3>\n";
  echo "<table border='1'>\n";
  for ($i=0;$i<$n;$i++) {
    echo "  <tr>\n";
    for ($j=0;$j<5;$j++) {
      if ($machine = mysql_fetch_array($machResult)) {
        // Get the recommendations from the most recent survey for this machine
        $query = "SELECT  FROM  WHERE MachineID=".$machine['MachineID'] . " ORDER BY TestDate DESC LIMIT 1";
        $machTestDate = mysql_fetch_array(mysql_query($query));
        $days = $machTestDate['days'];
        // Set the table cell ID based on the number of days since the last survey
        if ($days < 335) {
          // Survey is current. < 11 mo since last survey
          echo "    <td class='green'>\n";
        }
        elseif (($days >= 335) && ($days < 365)) {
          // Survey is due within 30 days
          echo "    <td class='yellow'>\n";
        }
        elseif (($days >= 365) && ($days < 395)) {
          // Survey is overdue but less than 30 days overdue
          echo "    <td class='orange'>\n";
        }
        elseif ($days > 395) {
          // Suvey is more than 13 months overdue
          echo "    <td class='red'>\n";
        }
        else {
          echo "    <td>\n";
        }
        // TODO Link description to report page for that machine
        echo "<a href=\"RepMachine.php?MachID=" . $machine['MachineID'] . "\">". $machine['Description']."</a><br />\n";
        echo $machine['Location']."<br />\n";
        // TODO Link test date to survey recommendations page for that survey
        echo "<a href=\"SurveyResRec.php?SurveyID=" . $machTestDate['SurveyID'] . "\">" . $machTestDate['TestDate'] . "</a><br />\n";
//        echo $machine['MachineID']."<br />\n";
//        echo $days."<br />\n";
        echo "    </td>\n";
      }
      else break;
    }
    echo "  </tr>\n";
  }
  echo "</table>\n";
}

?>
<h3>Table legend</h3>
<table>
  <tr>
    <td class="green">No unresolved recommendations</td>
    <td class="yellow">Unresolved recommendations < 30 days</td>
    <td class="orange">Unresolved recommendations > 30 days</td>
    <td class="red">Unresolved recommendations > 60 days</td>
  </tr>
</table>
<a href="http://radinfo.musc.edu/ComplianceReports/" target="_top">Main</a>
</body>

</html>
